package com.schillerchina.baselibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

/**
 * @author zhangzhiguo
 * @date 2020/7/12 3:42 PM
 * @Description:
 */
public class WindowsUtils {

    private static final String TAG = WindowsUtils.class.getSimpleName();

    private Context mContext;
    private WindowManager mWindowManager;
    private DisplayMetrics mMetrics;

    public WindowsUtils(Context context) {
        this.mContext = context;
        this.mWindowManager = (WindowManager) this.mContext.getSystemService(Context.WINDOW_SERVICE);
        this.mMetrics = new DisplayMetrics();
        this.mWindowManager.getDefaultDisplay().getMetrics(this.mMetrics);
    }

    /**
     * 获取密度点
     *
     * @return
     */
    public float getDensity() {
        return this.mMetrics.density;
    }

    /**
     * 获取高度
     *
     * @return
     */
    public int getWindowHeight() {
        return this.mMetrics.heightPixels;
    }

    /**
     * 获取屏幕宽度
     *
     * @return
     */
    public int getWindowWidth() {
        return this.mMetrics.widthPixels;
    }

    public DisplayMetrics getmMetrics() {
        return this.mMetrics;
    }

    private float getWindowSize() {
        float density = this.mMetrics.density;
        float xdpi = this.mMetrics.xdpi;
        float widthPixels = (float) this.mMetrics.widthPixels;
        float heightPixels = (float) this.mMetrics.heightPixels;
        float z = (float) Math.sqrt(Math.pow((double) widthPixels, 2.0D) + Math.pow((double) heightPixels, 2.0D));
        float size = z / xdpi;
        return size;
    }

    public boolean isSmallWindow() {
        float size = this.getWindowSize();
        return size <= 6.0F;
    }

    /**
     * 判断横竖屏
     *
     * @return
     */
    public boolean isHorizontal() {
        return this.getWindowWidth() > this.getWindowHeight();
    }

    public void fixOrientation() {
        Activity mActivity = (Activity) this.mContext;
        if (this.isHorizontal()) {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else {
            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        }

    }

    public void setOrientationToSensor() {
        Activity mActivity = (Activity) this.mContext;
        mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }

    /**
     * 计算每个小格大小
     *
     * @return
     */
    public float getSampleCellWidth() {
        float cellWidth = 0.0F;
        int window_w;
        if (this.isHorizontal()) {
            window_w = this.getWindowHeight();
        } else {
            window_w = this.getWindowWidth();
        }

        int num = 255;
        if (window_w < 600) {
            num = 190;
        }
//        cellWidth = 6
        cellWidth = (float) (window_w / num);

        Log.i(TAG, "cellWidth:" + cellWidth);

        return cellWidth;
    }
}
